Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
    mobile: '',
    description: '',
    houseId: '',
  },
  onLoad({ id }) {
    this.getHouseList()
    this.getRepairItem()
    if (id) {
      wx.setNavigationBarTitle({
        title: '编辑报修信息',
      })
      this.getRepairDetail(id)
    }
  },
  /**若有传入的id 获取已填写过的信息 */
  async getRepairDetail(id) {
    const { data, code } = await wx.http.get('/repair/' + id)
    if (code !== 10000) return wx.utils.toast('获取报修信息错误')
    console.log(data)
    this.setData({ ...data })
  },

  /** 获取房屋列表 */
  async getHouseList() {
    const { data: houseList, code } = await wx.http.get('/house')
    if (code !== 10000) return wx.utils.toast('获取房屋列表出错')
    this.setData({
      houseList,
    })
  },
  /** 获取维修列表 */
  async getRepairItem() {
    const { code, data: repairItem } = await wx.http.get('/repairItem')
    if (code !== 10000) wx.utils.toast('获取维修项目失败')
    this.setData({
      repairItem,
    })
  },
  selectHouse(ev) {
    const { name: houseInfo, id: houseId } = ev.detail
    this.setData({
      houseInfo,
      houseId,
    })
  },
  selectRepairItem(ev) {
    const { name: repairItemName, id: repairItemId } = ev.detail
    this.setData({
      repairItemName,
      repairItemId,
    })
  },
  selectDate(ev) {
    const appointment = wx.utils.formatTime(ev.detail)
    this.setData({ appointment })
    // 关闭弹层
    this.closeDateLayer()
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },

  afterRead(ev) {
    wx.showLoading({
      title: '正在上传',
      mask: true,
    })
    // 拿到临时文件
    const { file } = ev.detail
    // 上传到服务器
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: getApp().token,
      },
      success: (res) => {
        // 转换JSON数据
        const data = JSON.parse(res.data)
        if (data.code !== 10000) return wx.utils.toast(data.message)
        const { attachment } = this.data
        attachment.push({ ...data.data })
        this.setData({ attachment })
      },
      complete() {
        wx.hideLoading()
      },
    })
  },
  // 校验函数
  verifyHouse() {
    const valid = this.data.houseId !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择房屋信息!')
    // 返回验证结果
    return valid
  },
  verifyRepair() {
    const valid = this.data.repairItemId !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择维修项目!')
    // 返回验证结果
    return valid
  },
  verifyMobile() {
    // 验证手机号
    const reg = /^[1][3-8][0-9]{9}$/
    const valid = reg.test(this.data.mobile.trim())
    // 验证结果提示
    if (!valid) wx.utils.toast('请填写正确的手机号码!')
    // 返回验证结果
    return valid
  },
  verifyDate() {
    // 验证日期格式
    const reg = /^\d{4}\/\d{2}\/\d{2}$/
    const valid = reg.test(this.data.appointment)
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择预约日期!')
    // 返回验证结果
    return valid
  },
  verifyDescription() {
    // 验证报修项目描述
    const valid = this.data.description.trim() !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请填写问题描述!')
    // 返回验证结果
    return valid
  },
  async submit() {
    // 校验
    if (!this.verifyHouse()) return
    if (!this.verifyRepair()) return
    if (!this.verifyMobile()) return
    if (!this.verifyDate()) return
    if (!this.verifyDescription()) return

    // 获值
    const { id, houseId, repairItemId, mobile, appointment, description, attachment } = this.data
    const sub = { id, houseId, repairItemId, mobile, appointment, description, attachment }

    // 提交请求
    const { code } = await wx.http.post('/repair', sub)
    if (code !== 10000) {
      return wx.utils.toast('提交失败')
    }
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
